//对axios进行二次封装
import axios from "axios";
// import Nprogress from "nprogress";
// import 'nprogress/nprogress.css'
import {ElMessage } from 'element-plus';
import { ElLoading  } from 'element-plus';
import { ref } from "vue";
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
//配置基础路径和超时时间
const service = axios.create({
    withCredentials:true,// 跨域请求时发送Cookie
    baseURL:'/api',
    timeout:20000
})


//配置请求拦截，响应拦截
service.interceptors.request.use(
    (config:any) => {
        //在请求发送之前做一些事情
        // Nprogress.start()   //添加进度条   nprogress
        // return config   //config本质是拦截的请求报文，我们可以在这里处理请求报文
        //可以在这里添加额外功能
        //在发送请求之前做些什么
        const token = localStorage.getItem("token")
        if (token) {
            config.headers.Authorization = token
        }
        return config
    }
)

service.interceptors.response.use(
    (response:any) => {
        // Nprogress.done()    //响应结果出来之后无论成功还是失败都要关闭i进度条
        //返回的响应不再需要从data属性当中那数据，而是响应就是我们要的数据
        return response.data
    },
    (error) => {
        // Nprogress.done()    //响应结果出来之后无论成功还是失败都要关闭i进度条
        // 统一处理请求错误，
        alert('发送ajax请求失败' + error.message || '未知错误')
        //统一处理后，后期不再处理
        // return new Promise(() => {}) //中断promise链
        //统一处理后，后期还想进一步处理
        return Promise.reject(new Error('failed'))
    }
)


export default {
get(url:string, data:object, header:{}) {
    return new Promise((resolve) => {
        //全局loading
        const loadingInstance = ElLoading.service({ fullscreen: true,background: 'rgba(0, 0, 0, 0.7)',text: '请稍后' })
        service.get(url, {
            data: data
        },header).then(res => {
            if(res.data.code==9000){
                ElMessage.error('登录失效，请重新登录');
                router.replace({path:'/login'})
            }
            resolve(res)
        }, err => {
            if (err.response) {
            ElMessage ({
                    showClose: true,
                    message: err.response.data.message,
                    type: 'error'
                });
            }
        })
    })
},
post(url:string, data:object, header:{}) {
    return new Promise((resolve) => {
        //全局loading
        const loadingInstance = ElLoading.service({ fullscreen: true,background: 'rgba(0, 0, 0, 0.7)',text: '请稍后' })
        service.post(url, data, header).then(res => {
        if(res.data.code==9000){
            ElMessage.error('登录失效，请重新登录');
            router.replace({path:'/login'})
        }
            resolve(res);

        }, err => {
            if (err.response) {
            ElMessage ({
                    showClose: true,
                    message: err.response.data.message,
                    type: 'error'
                });
            }
        })
    })
},
postNL(url:string, data:object, header:{}) {
return new Promise((resolve) => {
    service.post(url, data, header).then(res => {
    if(res.data.code==9000){
        ElMessage.error('登录失效，请重新登录');
        router.replace({path:'/login'})
    }
        resolve(res);

    }, err => {
        if (err.response) {
        ElMessage ({
                showClose: true,
                message: err.response.data.message,
                type: 'error'
            });
        }
    })
})
},
put(url:string, data:object, header:{}) {
    return new Promise((resolve) => {
        //全局loading
            const loadingInstance = ElLoading.service({ fullscreen: true,background: 'rgba(0, 0, 0, 0.7)',text: '请稍后' })
            service.put(url, data, header).then(res => {
            if(res.data.code==9000){
                ElMessage.error('登录失效，请重新登录');
                router.replace({path:'/login'})
            }
                resolve(res);

            }, err => {
                if (err.response) {
                ElMessage ({
                    showClose: true,
                    message: err.response.data.message,
                    type: 'error'
                });
            }
        })
    })
},
delete(url:string, data:object, header:{}) {
    return new Promise((resolve) => {
        //全局loading
        loadingInstance = ElLoading.service({ fullscreen: true,background: 'rgba(0, 0, 0, 0.7)',text: '请稍后' })
        service.delete(url, data, header).then(res => {
            if(res.data.code==9000){
                ElMessage.error('登录失效，请重新登录');
                router.replace({path:'/login'})
            }
                resolve(res);
            }, err => {
                if (err.response) {
                ElMessage ({
                        showClose: true,
                    message: err.response.data.message,
                    type: 'error'
                });
            }
        })
    })
},
Lpost(url:string, data:object, header:{}) {
    return new Promise((resolve) => {
        //全局loading
        loadingInstance = ElLoading.service({ fullscreen: true,background: 'rgba(0, 0, 0, 0.7)',text: '请稍后' })
        service.post(url, data,header)
        .then(res => {
            if(res.data.code==9000){
                ElMessage.error('登录失效，请重新登录');
                router.replace({path:'/login'})
            }
                resolve(res);

            }, err => {
                if (err.response) {
                ElMessage ({
                    showClose: true,
                    message: err.response.data.message,
                    type: 'error'
                });
            }
        })
    })
},
download(url:string, data:object, header:{}) {
    return new Promise((resolve, reject) => {
        axios({
            method: 'get',
            url: url,
            data: data,
            responseType: 'blob', // 注意返回的数据格式
            headers:header
            }).then(res => {
            if(res.data.code==9000){
                ElMessage.error('登录失效，请重新登录');
                router.replace({path:'/login'})
            }
            resolve(res)
            }).catch(error => {
            reject(error)
            })
        })

},
postDownload(url:string, data:object, header:string) {
  return new Promise((resolve, reject) => {
      axios({
        method: 'post',
        url: url,
        data: data,
        responseType: 'blob', // 注意返回的数据格式
        headers:header
      }).then(res => {
        if(res.data.code==9000){
            ElMessage.error('登录失效，请重新登录');
            router.replace({path:'/login'})
        }
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
},
}